package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.FamilyInformation;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.domain.WorkInformation;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDetailInformationService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IFamilyInformationService;
import cn.wolfcode.crm.service.IWorkInformationService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IDetailInformationService detailInformationService;
    @Autowired
    private IFamilyInformationService familyInformationService;
    @Autowired
    private IWorkInformationService workInformationService;


    public void insert(Employee record) {
        Md5Hash md5Hash = new Md5Hash(record.getPassword(), record.getUsername(), 2);
        record.setPassword(md5Hash.toString());
        employeeMapper.insert(record);
        List<Role> roles = record.getRoles();
        if (roles != null) {
            for (Role role : roles) {
                employeeMapper.saveRelation(record.getId(), role.getId());
            }
        }
    }
    public void updateByPrimaryKey(Employee record) {
        employeeMapper.updateByPrimaryKey(record);
        employeeMapper.deleteRelation(record.getId());
        List<Role> roles = record.getRoles();
        if (roles != null) {
            for (Role role : roles) {
                employeeMapper.saveRelation(record.getId(), role.getId());
            }
        }
        if(record.getDetailInformation() != null && record.getDetailInformation().getId() ==null){
            record.getDetailInformation().setEmployeeId(record.getId());
            detailInformationService.insert(record.getDetailInformation());
        }else if(record.getDetailInformation() != null && record.getDetailInformation().getId() !=null){
            record.getDetailInformation().setEmployeeId(record.getId());
            detailInformationService.updateByPrimaryKey(record.getDetailInformation());
        }

        List<FamilyInformation> familyInforns = record.getFamilyInformations();

        if (familyInforns != null) {
            for (FamilyInformation familyInforn : familyInforns) {
                if(familyInforn.getId() == null){
                familyInforn.setEmployeeId(record.getId());
                familyInformationService.insert(familyInforn);
                }else{
                    familyInforn.setEmployeeId(record.getId());
                    familyInformationService.updateByPrimaryKey(familyInforn);
                }
            }
        }
        List<WorkInformation> workInforns = record.getWorkInformations();

        if (workInforns != null) {
            for (WorkInformation workInforn : workInforns) {
                if(workInforn.getId() == null){
                    workInforn.setEmployeeId(record.getId());
                    workInformationService.insert(workInforn);
                }else{
                    workInforn.setEmployeeId(record.getId());
                    workInformationService.updateByPrimaryKey(workInforn);
                }
            }
        }


    }

    public PageResult query(QueryObject qo) {
        int total = employeeMapper.query4Count(qo);
        if (total > 0) {
            return new PageResult(total, employeeMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public Employee getByUsername(String username)
    {
        return employeeMapper.getByUsername(username);
    }

    public void changState(Long id) {
        employeeMapper.changState(id);
    }

    public void resetPassword(String newPassword, Long id) {

        employeeMapper.resetPassword(newPassword, id);
    }

    public List<Long> getRoleByEmployeeId(Long employeeId)
    {
        return employeeMapper.getRoleByEmployeeId(employeeId);
    }

    public HSSFWorkbook exportXls(QueryObject qo) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("employee");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("邮箱");
        row.createCell(4).setCellValue("部门");
        row.createCell(5).setCellValue("入职时间");
        row.createCell(6).setCellValue("状态");
        List<Employee> employees = employeeMapper.selectXls(qo);
        if (employees.size() > 0) {
            for (int i = 0; i < employees.size(); i++) {
                Employee employee = employees.get(i);
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(employee.getUsername());
                row.createCell(1).setCellValue(employee.getRealname());
                row.createCell(2).setCellValue(employee.getTel());
                row.createCell(3).setCellValue(employee.getEmail());
                row.createCell(4).setCellValue(employee.getDept().getName());
                //设置导出的日期的格式
                HSSFCell cell = row.createCell(5);
                cell.setCellValue(new Date());
                HSSFCellStyle cellStyle = workbook.createCellStyle();

                HSSFDataFormat format = workbook.createDataFormat();

                cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));

                cell.setCellStyle(cellStyle);
                if (employee.getAdmin()) {
                    row.createCell(6).setCellValue("超级管理员");
                } else {
                    row.createCell(6).setCellValue("普通用户");
                }
            }
        }
        return workbook;
    }

    public void importXls(MultipartFile file) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        HSSFSheet sheet = workbook.getSheet("day01");
        int i = 0;
        for (Row cells : sheet) {
            if (i == 0) {
                i++;
                continue;
            }
            String username = cells.getCell(0).getStringCellValue();
            String realname = cells.getCell(1).getStringCellValue();
            String tel = cells.getCell(2).getStringCellValue();
            String email = cells.getCell(3).getStringCellValue();
            String dept_name = cells.getCell(4).getStringCellValue();
            Date hireDate = cells.getCell(5).getDateCellValue();
            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setRealname(realname);
            employee.setTel(tel);
            employee.setHireDate(hireDate);
            String admin = cells.getCell(6).getStringCellValue();
            if("超级管理员".equals(admin)){
                employee.setAdmin(true);
            }else{
                employee.setAdmin(false);
            }
            employeeMapper.insert(employee);
        }
    }

    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    public List<Employee> getByTeacherRoleSn(String teacherRoleSn) {
        return employeeMapper.getByTeacherRoleSn(teacherRoleSn);
    }

    public List<Employee> getSaleMen() {
        return employeeMapper.getSaleMen();
    }

    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
}
